package com.sxf.controller.blog.admin;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.servlet.ModelAndView;

import com.sxf.common.tools.Constants;
import com.sxf.controller.SuperController;
import com.sxf.domain.UserVO;
import com.sxf.formbean.UserForm;
import com.sxf.querybean.UserQuery;
import com.sxf.service.UserService;
import com.sxf.util.PasswordUtils;
import com.sxf.util.SysLogUtil;

@Controller
@RequestMapping("blog/login")
public class LoginController extends SuperController {

	@Resource(name = "userService")
	private UserService us;

	@RequestMapping(value = "", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView showLogin() throws Exception {
		Map<String, Object> map = new ModelMap();
		map.put("loginForm", new UserForm());
		ModelAndView mav = new ModelAndView();
		// mav = new ModelAndView(new RedirectView(""), map);
		// mav.setView(new RedirectView(""));//重定向
		mav.addAllObjects(map);
		mav.setViewName("blog/admin/login");
		return mav;
	}

	/**
	 * <code>@ModelAttribute("user")</code>映射表单bean
	 * <p/>
	 * 如果view采用JSP的话<code>@ModelAttribute</code>
	 * 注解可以省略，但是如果是使用Velocity或FreeMarker 的话必须要加上，而且<code>@Valid</code>
	 * 注解和后面的BindingResult类型的参数之间不能有其他参数。
	 * 
	 * @param user
	 * @return
	 */

	@RequestMapping(value = "/doLogin", method = { RequestMethod.GET,
			RequestMethod.POST })
	public ModelAndView doLogin(
			@Valid @ModelAttribute("loginForm") UserForm uF, BindingResult br,
			Map<String, Object> model, WebRequest request) throws Exception {
		ModelAndView mav = new ModelAndView();
		ModelMap modelMap = new ModelMap();
		String url = "blog/admin/login";
		if (br.hasErrors()) {
			Map<String, Object> map = br.getModel();
			List<ObjectError> oe = br.getAllErrors();
			for (ObjectError oer : oe) {
				SysLogUtil.info(oer.getDefaultMessage());
			}
			for (Iterator<String> it = map.keySet().iterator(); it.hasNext();) {
				String key = it.next();
				SysLogUtil.debug("KEY:" + key + ",Value=" + map.get(key));

			}
			mav.addAllObjects(br.getModel());
		} else if (StringUtils.hasText(uF.getLoginName())
				&& StringUtils.hasText(uF.getPassword())) {
			UserQuery userQuery = new UserQuery();
			userQuery.setLoginName(uF.getLoginName());

			String encodeStr = PasswordUtils.encode(uF.getPassword(),
					uF.getLoginName() + uF.getPassword());
			userQuery.setPassword(encodeStr);
			List<UserVO> resultUser = us.queryList(userQuery);
			if (resultUser != null && !resultUser.isEmpty()) {
				UserVO user = resultUser.get(0);
				user.getUserMetaSet();
				// request.setAttribute(Constants.LOGIN_USER, resultUser.get(0),
				// WebRequest.SCOPE_SESSION);
				modelMap.put(Constants.LOGIN_USER, user);
				mav.addAllObjects(modelMap);
				url = "redirect:/blog/admin";
			}
		}

		// return "redirect:/blog/login/show";		
		mav.setViewName(url);
		return mav;
	}

}
